﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;

namespace EntityMap.Meta
{
    /// <summary>
    /// Base class for all types of sql statements.
    /// </summary>
    public abstract class SqlStatement
    {
        [XmlAttribute( "id" )]
        public string Id { get;set; }

        [XmlElement("command")]
        public string Command { get; set; }

        [XmlAttribute( "parameterMap" )]
        public string ParameterMap { get; set; }

        public virtual SqlCommand GetSqlCommand( SqlConnection connection )
        {
            SqlCommand cmd = connection.CreateCommand();

            cmd.CommandText = Command;
            cmd.CommandType = CommandType.Text;

            return cmd;
        }

        public SqlCommand GetSqlCommand( SqlConnection connection, SqlParameter[] parameters )
        {
            SqlCommand cmd = this.GetSqlCommand( connection );

            if( parameters != null )
            {
                cmd.Parameters.AddRange( parameters );
            }

            return cmd;
        }

        public abstract object Process<T>( Context context );

        public override string ToString()
        {
            return String.Format( "id:{0} - command:{1}", Id, Command );
        }
    }
}
